#ifndef TREENODE_H
#define TREENODE_H
class Tree;
class TreeNode{
public:
    TreeNode(int _val = 0, TreeNode* _left=nullptr,TreeNode *_right =nullptr):val(_val),left(_left),right(_right){}
    virtual ~TreeNode(){}
    /* 获取和设置左右节点 */
    inline TreeNode *getLeftChild(){ return left;}
    inline TreeNode *getRightChild() {return right;}
    static TreeNode *getLeftChild(TreeNode *node){
        if(node == nullptr) return nullptr;
        return node->left;
    }
    static TreeNode *getRightChild(TreeNode *node){
        if(node == nullptr) return nullptr;
        return node->right;
    }

    inline TreeNode *setLeftChild(TreeNode *node){
        TreeNode* n = this->left;
        this->left = node;
        return n;
    }
    inline TreeNode *setRightChild(TreeNode *node){
        TreeNode* n = this->right;
        this->right = node;
        return n;
    }

    static TreeNode *setLeftChild(TreeNode *node, TreeNode *left){
        if(node == nullptr) return nullptr;

        TreeNode* l = node->left;
        node->left = left;
        return l;
    }

    static TreeNode *setRightChild(TreeNode *node, TreeNode *right){
        if(node == nullptr) return nullptr;

        TreeNode* r = node->right;
        node->right = right;
        return r;
    }

    int getVal() const { return val; }
    void setVal(int val){ this->val = val; }
    friend class Tree;
protected:
    int val;
    TreeNode *left;
    TreeNode *right;
};

#endif // TREENODE_H
